

jQuery(function ($) {
    var url = window.location.pathname;
    var pageOnOff = true;
    getList(0);
    var onoff = false;
    //分页按钮
    function twbsPagination(data) {
       
        Page({
			num:Math.ceil(data / 20),					//页码数
			startnum:1,				//指定页码
			elem:$('#pagination_b'),		//指定的元素
			callback:function(page){	//回调函数
                if (page != 1) {
                    onoff = true;
                }
                if (onoff) {
                    $('.content_list').html('');
                    getList(page - 1);
                    $('html,body').animate({scrollTop:$('#main ').offset().top-80}, 500);
                }
			}
		});
    };
    //专题列表
    function getList(page) {
        if (url.indexOf('.html') == -1) {
            var keyworrd = url.split('/')[2];
            var keyParam = {
                "ArticleCat": {
                    "url": keyworrd
                },
                "Article[]": {
                    "query": 2,
                    "count": 8,
                    "page": page,
                    "Article": {
                        "cat_id@": "ArticleCat/id",
                        "@order": "rank-"
                    }
                },
                "total@": "Article[]/total"
            };
        } else {
            var keyworrd = window.location.search.split('=')[1];
            var keyParam = {
                "ArticleCat": {
                    "id": keyworrd
                },
                "Article[]": {
                    "query": 2,
                    "count": 8,
                    "page": page,
                    "Article": {
                        "cat_id@": "ArticleCat/id",
                        "@order": "rank-"
                    }
                },
                "total@": "Article[]/total"
            };
        };
        send_ajax('post', '/get', keyParam, function callback(res) {
            if (res.code == 200 && res["Article[]"]) {
                var keywordHtml = "";
                if (res.ArticleCat) {
                    var search_keyword = res.ArticleCat.tdk_search_keywords.split('，');
                    if (search_keyword.length > 0) {
                        for (var j = 0; j < search_keyword.length; j++) {
                            keywordHtml += '   <li><a href="/goods/goods_search.html?keyword=' + search_keyword[j] + '" target="_blank">' + search_keyword[j] + '</a></li>\n                                \n                                ';
                        }
                        $('.list_nav').html(keywordHtml);
                    }
                    document.title = res.ArticleCat.tdk_title;
                    $('#search').attr('placeholder', res.ArticleCat.tdk_placeholder);
                    // $('.nav_title').html(res.ArticleCat.name);
                    $('.notice_content .title').html(res.ArticleCat.name);
                }

                //分页按钮
                if(pageOnOff){
                    twbsPagination(res.total);
                    pageOnOff=false;
                }
                var data = res["Article[]"];
                var html = "";
                for (var i = 0; i < data.length; i++) {
                    data[i].content = data[i].content.replace(/(\n)/g, "");
                    data[i].content = data[i].content.replace(/(\t)/g, "");
                    data[i].content = data[i].content.replace(/(\r)/g, "");
                    data[i].content = data[i].content.replace(/<\/?[^>]*>/g, "");
                    data[i].content = data[i].content.replace(/\s*/g, "");
                    html += '<li>\n                        <p class="li_title">\n                            <span class="title_left">\n                                ' + data[i].title + "\n                            </span>\n                            <span class=\"time\">\n                                \u3010\u6613\u4ECE\u8D44\u8BAF\u3011 " + data[i].create_time.split('.')[0] + " \u66F4\u65B0\n                            </span>\n                        </p>\n                        <p class=\"li_content\">\n                            <span>" + data[i].content + '</span>\n                            <a href="/article-' + data[i].id + ".html\" target=\"_blank\">\n                                <span class=\"red\">[\u5168\u6587]</span>\n                            </a>\n                            \n                        </p>\n                    </li>";
                };
                $('.content_list').html(html);
            }
        });
    };
    var artivle_nav = {
        "ArticleCat[]": {
            "ArticleCat": {
                "use_flag": 1,
                "@order": "rank-",
                "ArticleCat[]": {
                    "ArticleCat": {
                        "@order": "rank-",
                        "parent_id@": "ArticleCat[]/ArticleCat/id"
                    }
                }
            }
        }
    };
    send_ajax('post', '/get', artivle_nav, function callback(res) {
        var html = "";
        if (res.code == 200&&res['ArticleCat[]']) {
            var data = res['ArticleCat[]'];
            for (var i = 0; i < data.length; i++) {
                if (!data[i].parent_id) {
                    html += ' <div class="small_box">\n                                <div class="left">\n                                    <div class="font">\n                                        <p>' + data[i].name + '</p>\n                                    </div>\n                                </div>\n                                <div class="right" data-id=' + data[i].id + '>\n                                </div>\n                        </div>';
                }
            };
            $('.nav_box').html(html);
            var option = $('.small_box .right');
            for (var n = 0; n < option.length; n++) {
                for (var m = 0; m < data[n]['ArticleCat[]'].length; m++) {
                    var sort = data[n]['ArticleCat[]'][m];
                    if ($(option[n]).attr('data-id') == sort.parent_id) {
                        $(option[n]).append('<a target="_blank" href="/article/article_more.html?cat_id=' + sort.id + ('">' + sort.name + '</a>'));
                    }
                }
            }
        }
    });
});